package com.platform.task;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.platform.common.core.text.Convert;
import com.platform.common.utils.netty.ChannelSupervise;
import com.platform.common.utils.netty.NettyUtil;
import com.platform.common.utils.redis.RedisRun;
import com.platform.common.utils.redis.RedisUtil;
import com.platform.module.domain.*;
import com.platform.module.service.IBaseCalorimeterDataService;
import com.platform.module.service.IBaseCalorimeterTypeService;
import com.platform.module.service.ICalorRecordService;
import com.platform.module.service.ILinkConcentratorMeterService;
import io.netty.channel.Channel;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;
import java.util.concurrent.CountDownLatch;

/**
 * 热表定时调度任务
 *
 * @author platform
 */
@Component("calorTask")
public class CalorTask {

    @Autowired
    private RedisUtil redisUtil;

    @Autowired
    private IBaseCalorimeterDataService baseCalorimeterDataService;

    @Autowired
    private ILinkConcentratorMeterService linkConcentratorMeterService;

    /**
     * 根据热表id采集热表数据
     * @param calorIds
     */
    public void calorTaskById(String calorIds) {
        QueryWrapper queryWrapper = new QueryWrapper();
        queryWrapper.in("calorimeter_id", Arrays.asList(Convert.toStrArray(calorIds)));
        List<LinkConcentratorMeter> calorimeterList = linkConcentratorMeterService.list(queryWrapper);
        for(LinkConcentratorMeter concentratorMeter : calorimeterList){
            //获取采集器高位
            String cenhigh = concentratorMeter.getConcentratorId().substring(0,2);
            //获取采集器低位
            String cenlow = concentratorMeter.getConcentratorId().substring(2,4);
            //判断通道是否存在
            if(!redisUtil.hHasKey("sys_netty", cenhigh+cenlow)){
                break;
            }
            //获取通道id
            String channelId = redisUtil.hget("sys-netty",cenhigh+cenlow).toString();
            Channel channel = ChannelSupervise.findChannel(channelId);
            if(channel==null){
                break;
            }

            String add3 = concentratorMeter.getCalorimeterId().substring(0,2);
            String add4 = concentratorMeter.getCalorimeterId().substring(2,4);
            String add5 = concentratorMeter.getCalorimeterId().substring(4,6);
            String add6 = concentratorMeter.getCalorimeterId().substring(6,8);
            //控制码
            String cont = "01";
            //获取设备厂商编码
            BaseCalorimeterData baseCalorimeterData = baseCalorimeterDataService.getById(concentratorMeter.getCalorimeterId());
            String factory = baseCalorimeterData.getFactory().getFactoryNumber();
            //计算校验码
            int ll = NettyUtil.get10HexNum(cenhigh)+NettyUtil.get10HexNum(cenlow)+NettyUtil.get10HexNum(add3)+NettyUtil.get10HexNum(add4)+NettyUtil.get10HexNum(add5)+NettyUtil.get10HexNum(add6)+NettyUtil.get10HexNum(cont)+NettyUtil.get10HexNum(factory);
            String check = NettyUtil.get16HexNum(ll);
            if(check.length()<2){
                check = "0" + check;
            }else{
                check = check.substring(check.length()-2);
            }
            String response = "AA "+ cenlow +" "+ cenhigh + " " + add3 + " " + add4 + " " + add5 + " " + add6 + " " + cont + " " + factory + " 00 00 00 00 00 " + check + " FF ";
            String revertStr = response.replace(" ", "");
            NettyUtil.writeToClient(revertStr,channel,"读取热表数据");
        }
    }

    /**
     * 采集所有热表数据
     * @throws ParseException
     */
    public void calorTask() {
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        List<LinkConcentratorMeter> calorimeterList = linkConcentratorMeterService.list();
        for(LinkConcentratorMeter concentratorMeter : calorimeterList){
            //获取采集器高位
            String cenhigh = concentratorMeter.getConcentratorId().substring(0,2);
            //获取采集器低位
            String cenlow = concentratorMeter.getConcentratorId().substring(2,4);
            //判断通道是否存在
            if(!redisUtil.hHasKey("sys_netty", cenhigh+cenlow)){
                break;
            }
            //获取通道id
            String channelId = redisUtil.hget("sys-netty",cenhigh+cenlow).toString();
            Channel channel = ChannelSupervise.findChannel(channelId);
            if(channel==null){
                break;
            }

            String add3 = concentratorMeter.getCalorimeterId().substring(0,2);
            String add4 = concentratorMeter.getCalorimeterId().substring(2,4);
            String add5 = concentratorMeter.getCalorimeterId().substring(4,6);
            String add6 = concentratorMeter.getCalorimeterId().substring(6,8);
            //控制码
            String cont = "01";
            //获取设备厂商编码
            BaseCalorimeterData baseCalorimeterData = baseCalorimeterDataService.getById(concentratorMeter.getCalorimeterId());
            String factory = baseCalorimeterData.getFactory().getFactoryNumber();
            //计算校验码
            int ll = NettyUtil.get10HexNum(cenhigh)+NettyUtil.get10HexNum(cenlow)+NettyUtil.get10HexNum(add3)+NettyUtil.get10HexNum(add4)+NettyUtil.get10HexNum(add5)+NettyUtil.get10HexNum(add6)+NettyUtil.get10HexNum(cont)+NettyUtil.get10HexNum(factory);
            String check = NettyUtil.get16HexNum(ll);
            if(check.length()<2){
                check = "0" + check;
            }else{
                check = check.substring(check.length()-2);
            }
            String response = "AA "+ cenlow +" "+ cenhigh + " " + add3 + " " + add4 + " " + add5 + " " + add6 + " " + cont + " " + factory + " 00 00 00 00 00 " + check + " FF ";
            String revertStr = response.replace(" ", "");
            NettyUtil.writeToClient(revertStr,channel,"读取热表数据");
        }
    }
}
